package whf.framework.util;

/**
 * Html属性编辑器，借助于StringBuilder实现
 * @author wanghaifeng
 *
 */
public class AttributeStringBuilder {
	private StringBuilder builder;
	
	private boolean ignoreEmptyValue;
	
	public AttributeStringBuilder(){
		builder = new StringBuilder();
	}
	
	public AttributeStringBuilder(boolean ignoreEmptyValue){
		builder = new StringBuilder();
		this.ignoreEmptyValue = ignoreEmptyValue;
	}
	
	public AttributeStringBuilder(int length){
		builder = new StringBuilder(length);
	}
	
	public AttributeStringBuilder(String text){
		builder = new StringBuilder(text);
	}
	
	public AttributeStringBuilder append(String name, String value){
		if(StringUtils.isEmpty(value) && this.ignoreEmptyValue) return this;
		if(value == null) value = "";
		builder.append(name).append("=\"").append(value).append("\" ");
		return this;
	}
	
	public AttributeStringBuilder append(String name, boolean value){
		builder.append(name).append("=\"").append(value).append("\" ");
		return this;
	}
	
	public AttributeStringBuilder append(String name, long value){
		builder.append(name).append("=\"").append(value).append("\" ");
		return this;
	}
	
	public AttributeStringBuilder append(String text){
		if(text != null && !"".equals(text)){
			builder.append(text);
		}
		return this;
	}
	
	public String toString(){
		return builder.toString();
	}
}
